package com.apps.gomdiem.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.apps.gomdiem.R;
import com.apps.gomdiem.helper.DataBaseHelper;
import com.apps.gomdiem.model.UserAccount;

public class LoginActivity extends Activity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_activity);
		
		Intent intent = new Intent(LoginActivity.this, MainActivity.class);
		startActivity(intent);
		finish();
		
		//xu ly button register
		Button register = new Button(this);//(Button) findViewById(R.id.btnRegister);
		register.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
				startActivity(intent);
			}
		});
		
		// Xu ly button login
		Button login = new Button(this);//(Button) findViewById(R.id.btnLogin);
		login.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				EditText username = (EditText) findViewById(R.id.username);
				String user=username.getText().toString();
				EditText password = (EditText) findViewById(R.id.password);
				String pass=password.getText().toString();
				
				DataBaseHelper db = new DataBaseHelper(getApplicationContext());
				ArrayList<UserAccount> listUserAccount = db.getAllUserAccount();
				
				for (int i = 0; i < listUserAccount.size(); i++) {
					String pUser =listUserAccount.get(i).mUserName;
					String pPass =listUserAccount.get(i).mPassword;
					if(user.equals(pUser)&& pass.equals(pPass)){
						Toast.makeText(getApplicationContext(), "Login success", Toast.LENGTH_LONG).show();
						Intent intent = new Intent(LoginActivity.this,MainActivity.class);
						intent.putExtra("username", pUser);
						startActivity(intent);
						finish();
					}
					else{
						Toast.makeText(getApplicationContext(), "Login fail", Toast.LENGTH_LONG).show();
					}
				}
			}
		});
	}
}